﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class FundamentalsMap : EntityTypeConfiguration<Fundamentals> {
        public FundamentalsMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_fundamentals");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.FundamentalsId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_FUN
            Property(a => a.SymbolId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_FUN", 1) { IsUnique = true }));
            Property(a => a.FundamentalDate)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_FUN", 2) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 1..n Sectors
            HasRequired<Sectors>(a => a.Sector)
                .WithMany()
                .HasForeignKey(a => a.SectorId);

            // 1..n Industries
            HasRequired<Industries>(a => a.Industry)
                .WithMany()
                .HasForeignKey(a => a.IndustryId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.FundamentalsId)
                .IsRequired()
                .HasColumnName("fun_id");

            Property(a => a.SymbolId)
                .IsRequired()
                .HasColumnName("fun_sym_id");

            Property(a => a.IndustryId)
                .IsRequired()
                .HasColumnName("fun_ind_id");

            Property(a => a.SectorId)
                .IsRequired()
                .HasColumnName("fun_sct_id");

            Property(a => a.FundamentalDate)
                .IsRequired()
                .HasColumnName("fun_dt");

            Property(a => a.Shares)
                .IsOptional()
                .HasColumnName("fun_shares");

            Property(a => a.MarketCap)
                .IsOptional()
                .HasColumnName("fun_market_cap");

            Property(a => a.PeRatio)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_pe");

            Property(a => a.PegRatio)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_peg");

            Property(a => a.EarningsPerShare)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_eps");

            Property(a => a.DividendYield)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_div_yield");

            Property(a => a.Ebitda)
                .IsOptional()
                .HasColumnName("fun_ebitda");

            Property(a => a.PriceToSales)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_pts");

            Property(a => a.PriceToBook)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_ptb");

            Property(a => a.Yield)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("fun_yield");
        }
    }
}
